package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.CateInfo;
import com.dao.CateDao;
import net.sf.json.JSONArray;


@WebServlet("/CateServlet.do")
public class CateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CateDao cateDao=new CateDao();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		String flag=request.getParameter("flag");
		
		
		if("addbigcate".equals(flag)){
			this.addBigCate(request,response);
		}
		
		else if("addsmallcate".equals(flag)){
			this.addSmallCate(request,response);
		}
		
		else if("manage".equals(flag)){
			this.manage(request,response);
		}
		
		else if("listsmallcate".equals(flag)){
			this.listSmallCate(request,response);
		}
		
		else if("delsmallcate".equals(flag)){
			this.delSmallCate(request,response);
		}
		
		else if("delbigcate".equals(flag)){
			this.delBigCate(request,response);
		}
		
		else if("updatecate".equals(flag)){
			this.updateCate(request,response);
		}
	}

	//更新分类信息(二级或一级均可
	private void updateCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int cateId=Integer.parseInt( request.getParameter("id"));
		int parentId=request.getParameter("bigCateId")==null?0:Integer.parseInt(request.getParameter("bigCateId"));
		String cateName=request.getParameter("cateName");
		String des=request.getParameter("des");
		
		CateInfo cateInfo=cateDao.getCateInfoById(cateId);
		cateInfo.setParentId(parentId);
		cateInfo.setCateName(cateName);
		cateInfo.setDes(des);
		
		if(cateDao.update(cateInfo)==1){
			request.setAttribute("cateInfo", cateInfo);  
			request.setAttribute("msg", "分类信息更新成功");
			
			String url;
			if(parentId==0){   //如果改的是一级分类,要转到一级分类修改页面
				url="bigcate_edit.jsp";
			}
			else{
				url="smallcate_edit.jsp";
			}
			
			request.getRequestDispatcher("/goods/"+url).forward(request, response);
		}	
	}

	//删除大分类
	private void delBigCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int cateId=Integer.parseInt(request.getParameter("id"));
		
		//删除大分类之前,要先确定该大分类型没有小分类才能删除
		int smallCateCount=cateDao.getSubCountByBigCate(cateId);
		if(smallCateCount==0){
			if(cateDao.delCateById(cateId)==1){
				request.setAttribute("msg", "一级分类删除成功!");
			}
		}
		else{
			request.setAttribute("msg","操作失败! 该分类下尚有"+smallCateCount+"个子分类,所以不能进行删除!");
		}
		
		this.manage(request, response);
	}

	//删除小分类
	private void delSmallCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int cateId=Integer.parseInt(request.getParameter("id"));
		
		//如果有商品在使用该小分类,则不允许删除此分类
		int goodsCount=cateDao.getGoodsCountBySmallCate(cateId);
		if(goodsCount==0){
			if(cateDao.delCateById(cateId)==1){
				request.setAttribute("msg", "二级分类删除成功!");
			}
		}
		
		else{
			request.setAttribute("msg", "操作失败! 现有 "+goodsCount+"种商品在该分类下,所以不能删除该分类");
		}
		
		this.manage(request, response);
		
	}

	//根据父级ID查询子分类(用于初始化下拉框)
	private void listSmallCate(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int bigCateId=Integer.parseInt(request.getParameter("bigcateid"));
		List<CateInfo> cateList=cateDao.getCateList(bigCateId);
		
		JSONArray jsonObj=JSONArray.fromObject(cateList);
		response.getWriter().print(jsonObj);
	}

	//商品分类管理(查询出列表以供维护)
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<CateInfo> cateList=cateDao.getCateList(0);
		request.setAttribute("cateList", cateList);
		request.getRequestDispatcher("/goods/cate_manage.jsp").forward(request, response);
	}

	//添加二级分类
	private void addSmallCate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
		String cateName=request.getParameter("cateName");
		String des=request.getParameter("des");
		
		CateInfo cateInfo=new CateInfo();
		cateInfo.setCateName(cateName);
		cateInfo.setDes(des);
		cateInfo.setParentId(bigCateId);
		
		if(cateDao.addCate(cateInfo)==1){
			request.setAttribute("msg", "二级分类添加成功");
			request.getRequestDispatcher("/goods/smallcate_add.jsp").forward(request, response);
		}	
	}

	//添加一级分类
	private void addBigCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cateName=request.getParameter("cateName");
		String des=request.getParameter("des");
		
		CateInfo cateInfo=new CateInfo();
		cateInfo.setCateName(cateName);
		cateInfo.setDes(des);
		cateInfo.setParentId(0);	
			
		if(cateDao.addCate(cateInfo)==1){
			request.setAttribute("msg", "一级分类添加成功");
			request.getRequestDispatcher("/goods/bigcate_add.jsp").forward(request, response);
		}	
	}

}
